package com.ruoyi.task.dynamicSched.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.ruoyi.task.dynamicSched.AbstractSchedulingConfigurer;
import com.ruoyi.task.dynamicSched.ScheduleTaskProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

public class JsonSchedulingConfigurer extends AbstractSchedulingConfigurer {

    // 这里把默认的任务配置JSON文件放在CLASSPATH下的scheduling/tasks.json,可以通过配置项scheduling.json.config.location进行覆盖
    @Value("${scheduling.json.config.location:scheduling/tasks.json}")
    private String location;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    protected List<InternalTaskProperties> loadTaskProperties() throws Exception {
        ClassPathResource resource = new ClassPathResource(location);
        String content = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
        ScheduleTaskProperties properties = objectMapper.readValue(content, ScheduleTaskProperties.class);
        if (Boolean.FALSE.equals(properties.getEnable()) || null == properties.getTasks()) {
            return Collections.emptyList();
        }
        List<InternalTaskProperties> taskList = Lists.newArrayList();
        for (ScheduleTaskProperties.ScheduleTasks tasks : properties.getTasks()) {
            List<ScheduleTaskProperties.ScheduleTaskMethod> taskMethods = tasks.getTaskMethods();
            if (null != taskMethods) {
                for (ScheduleTaskProperties.ScheduleTaskMethod taskMethod : taskMethods) {
                    if (!Boolean.FALSE.equals(taskMethod.getEnable())) {
                        if (ScheduleTaskProperties.ScheduleTaskType.CRON == taskMethod.getTaskType()) {
                            taskList.add(CronTaskProperties.builder()
                                    .taskMethod(taskMethod.getTaskMethod())
                                    .cronExpression(taskMethod.getCronExpression())
                                    .timeZone(taskMethod.getTimeZone())
                                    .taskDescription(taskMethod.getTaskDescription())
                                    .taskClass(tasks.getTaskClass())
                                    .build());
                        }
                        if (ScheduleTaskProperties.ScheduleTaskType.FIXED_DELAY == taskMethod.getTaskType()) {
                            taskList.add(FixedDelayTaskProperties.builder()
                                    .taskMethod(taskMethod.getTaskMethod())
                                    .intervalMilliseconds(taskMethod.getIntervalMilliseconds())
                                    .initialDelayMilliseconds(taskMethod.getInitialDelayMilliseconds())
                                    .taskDescription(taskMethod.getTaskDescription())
                                    .taskClass(tasks.getTaskClass())
                                    .build());
                        }
                        if (ScheduleTaskProperties.ScheduleTaskType.FIXED_RATE == taskMethod.getTaskType()) {
                            taskList.add(FixedRateTaskProperties.builder()
                                    .taskMethod(taskMethod.getTaskMethod())
                                    .intervalMilliseconds(taskMethod.getIntervalMilliseconds())
                                    .initialDelayMilliseconds(taskMethod.getInitialDelayMilliseconds())
                                    .taskDescription(taskMethod.getTaskDescription())
                                    .taskClass(tasks.getTaskClass())
                                    .build());
                        }
                    }
                }
            }
        }
        return taskList;
    }
}
